package com.ctvit.common;

import java.io.File;
import java.io.FileFilter;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;



import com.ctvit.struts.common.CommonFunc;

public class FileFilterTime implements FileFilter{
   static List<File> list = new ArrayList<File>();
   
   public FileFilterTime(){
	   
   }
   
   public static List<File> fileFilterTime(File[] pathname){
	   //得到循环的时间段
	   Date starttime = null,endtime;
	   
	   SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
	   
		try {
			starttime = DateTimeUtil.parseDate(CommonFunc.lastFileTime);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 
		
	   
	   endtime = Timestamp.valueOf(dformat.format(new Date()));  
	   
	 
	   //过滤出符合时间段条件的文件
	   for(File filename : pathname){
		   //得到文件的最后修改时间
		   if(filename.isDirectory()){
//				getFiles(filename.getPath());
			   continue;
		   }
			long m = filename.lastModified();
		
			Date filedate = getdate(m);
			//判断是否在循环的时间段内的文件
			boolean flagbef,flagaft;
			flagaft = filedate.after(starttime);
			flagbef = filedate.before(endtime);
		    if( flagaft && flagbef ){
//		    	System.out.println("filename==="+filename);
				list.add(filename);
			
		    	
		    }else{
		    	continue;
		    }
		}
	   
	   //记录此刻时间
	   String lastdate = DateTimeUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	   try {
		CommonFunc.setLastFileTime(lastdate);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
//	   System.out.println(lastdate);
	   return list;
   }

//   public static Date getdate(int i){ // //获取前后日期 i为正数 向后推迟i天，负数时向前提前i天 
//     Date dat = null; 
//     Calendar cd = Calendar.getInstance();  
//     cd.add(Calendar.DATE, i); 
//     dat = cd.getTime(); 
//     SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
//     Timestamp date = Timestamp.valueOf(dformat.format(dat));  
//     return date;  
//    } 
//
   //将秒转化为日期
   public static Date getdate(long time){
//       long time= 1293113853;//秒
       GregorianCalendar gc = new GregorianCalendar(); 
       gc.setTimeInMillis(time);
//       DateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//       System.out.println(df.format(gc.getTime()));
       return gc.getTime();
 }

@Override
	public boolean accept(File pathname) {
		// TODO Auto-generated method stub
		return false;
	}

	public static void main(String[] args){
		File[] array = new File("e:\\解析文件\\mam4ch5").listFiles();
		
		fileFilterTime(array);
		
		
//		System.out.println(getdate());
//		System.out.println(new Date());
//		System.out.println(getdate(-1).toString());
//		System.out.println(fileFilterTime(array).toString());		   
	}
}

